package com.hahaliu.hahaliu.service.impl.user;

import com.hahaliu.hahaliu.common.utils.RedisUtil;
import com.hahaliu.hahaliu.service.comment.CommentService;
import com.hahaliu.hahaliu.service.user.UserCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

@Service
public class UserCommentServiceImpl implements UserCommentService {

	@Autowired
	private RedisUtil redisUtil;

	@Autowired
	private CommentService commentService;

	@Autowired
	@Qualifier("taskExecutor")
	private Executor taskExecutor;

	/**
	 * 获取用户点赞和点踩的评论集合
	 * @param uid   当前用户
	 * @return  点赞和点踩的评论集合
	 */
	@Override
	public Map<String, Object> getUserLikeAndDislike(Integer uid) {
		Map<String, Object> map = new HashMap<>();
		// 获取用户点赞列表，并放入map中
		CompletableFuture<Void> userLikeFuture = CompletableFuture.runAsync(() -> {
			Object userLike = redisUtil.getMembers("user_like_comment:" + uid);
			if (userLike == null) {
				map.put("userLike", Collections.emptySet());
			} else{
				map.put("userLike", userLike);
			}
		}, taskExecutor);
		// 获取也用户点踩列表，并放入map中
		CompletableFuture<Void> userDislikeFuture = CompletableFuture.runAsync(() -> {
			Object userDislike = redisUtil.getMembers("user_dislike_comment:" + uid);
			map.put("userDislike", userDislike);
			if (userDislike == null) {
				map.put("userDislike", Collections.emptySet());
			} else {
				map.put("userDislike", userDislike);
			}
		}, taskExecutor);

		userDislikeFuture.join();
		userLikeFuture.join();

		return map;
	}
}
